package gupao_edu_spring_webmvc.web.servlet;

import gupao_edu_spring_webmvc.web.constants.Constants;

import java.io.File;
import java.util.Locale;

/**
 * @ClassName:
 * @Description:
 * @Auther: zhouych
 * @Date: 2019/5/9 20:25
 */
public class MYViewResolver {



    private File templateRootDir;

    public MYViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    public MYView resolveViewName(String viewName, Locale locale) throws Exception{
        if(null == viewName || "".equals(viewName.trim())){return null;}
        viewName = viewName.endsWith(Constants.DEFAULT_TEMPLATE_SUFFX) ? viewName : (viewName + Constants.DEFAULT_TEMPLATE_SUFFX);
        File templateFile = new File((templateRootDir.getPath() + "/" + viewName).replaceAll("/+","/"));
        return new MYView(templateFile);
    }
}
